//
//  TestTwoViewController.m
//  TestConstriant
//
//  Created by apple on 2018/7/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TestTwoViewController.h"

@interface TestTwoViewController ()
@property (weak, nonatomic) IBOutlet UITextField *timeTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (strong, nonatomic) IBOutlet UIPickerView *cityPicker;

@property(nonatomic,retain) NSMutableArray *dataArray;

@end

@implementation TestTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"TestPickerView";
    
    
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if (textField == self.timeTextField) {
        NSDate *date = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        
        if (date) {
            self.datePicker.date = date;
        }
        self.timeTextField.inputView = self.datePicker;
    }
    
    if (textField == self.cityTextField) {
        NSString * file =  [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
        self.dataArray = [NSMutableArray arrayWithContentsOfFile:file];
        self.cityTextField.inputView = self.cityPicker;
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.cityTextField) {
        NSInteger index = [self.cityPicker selectedRowInComponent:0];
        self.cityTextField.text = self.dataArray[index][@"content"];
    }
    if (textField == self.timeTextField) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        self.timeTextField.text = [dateFormatter stringFromDate:self.datePicker.date];
    }
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArray.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataArray[row][@"content"];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
